package com.example.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.dao.AskForLeaveDao;
import com.example.domain.AskForLeave;
import com.example.result.Result;
import com.example.result.ResultFactory;
import com.example.service.AskForLeaveService;
import com.example.tool.DateTool;

@Service
public class AskForLeaveServiceImpl implements AskForLeaveService {

    @Autowired
    AskForLeaveDao askForLeaveDao;

    /**
     * @describe 添加请假信息
     */
    @Override
    public Result addAskForLeave(AskForLeave askForLeave) {
        if (askForLeave != null) {
            // 获取系统当前时间作为记录创建时间
            askForLeave.setRecordTime(DateTool.getTimestamp());
        }
        // 调用JPA保存实体方法
        AskForLeave askForLeave1 = askForLeaveDao.save(askForLeave);

        if (askForLeave1 != null) {
            return ResultFactory.buildSuccessResult(askForLeave1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 更新请假信息
     */
    @Override
    public Result updateAskForLeave(AskForLeave askForLeave) {
        if (askForLeave != null) {
            // 获取系统当前时间作为记录更新时间
            askForLeave.setUpdateTime(DateTool.getTimestamp());
        }
        // 调用JPA保存并刷新实体方法
        AskForLeave askForLeave1 = askForLeaveDao.saveAndFlush(askForLeave);

        if (askForLeave1 != null) {
            return ResultFactory.buildSuccessResult(askForLeave1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据请假编号删除请假信息
     */
    @Override
    public Result deleteAskForLeaveById(String askId) {
        int row = askForLeaveDao.deleteByAskId(Integer.parseInt(askId));

        if (row > 0) {
            return ResultFactory.buildDefaultSuccessResult();
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 查询所有请假信息
     */
    @Override
    public Result queryAllAskForLeave() {
        List<AskForLeave> list = askForLeaveDao.findAll();
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据编号查询请假信息
     */
    @Override
    public Result queryAskForLeaveById(String askId) {
        List<AskForLeave> list = askForLeaveDao.findByAskId(Integer.parseInt(askId));
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 通过Excel批量添加请假信息
     */
    @Override
    public Result addAskForLeaveByExcel(List<AskForLeave> list) {
        List<AskForLeave> list1 = null;
        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setRecordTime(DateTool.getTimestamp());
            }
            list1 = askForLeaveDao.saveAll(list);
        }
        if (list1 != null) {
            return ResultFactory.buildSuccessResult(list1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据请假编号批量删除请假信息
     */
    @Override
    public Result deleteAskForLeaveByIds(Map<String, String> ids) {
        List<Integer> listId = new ArrayList<Integer>();
        int row = 0;
        if (ids != null) {
            String idstr = ids.get("Ids");
            String[] id = idstr.split(",");

            for (int i = 0; i < id.length; i++) {
                listId.add(Integer.parseInt(id[i]));

            }
        }
        System.out.println(listId);
        if (listId != null) {
            row = askForLeaveDao.deleteByAskIdIn(listId);
        }

        if (row > 0) {
            return ResultFactory.buildDefaultSuccessResult();
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 请假信息模糊查询
     */
    @Override
    public Result serachByInfo(String[] info) {
        List<Map<String, Object>> list = null;
//		System.out.println(info[0]);
        if (info[0].equals("duration")) {

            list = askForLeaveDao.findByDurationLike(info[1]);

        } else if (info[0].equals("reason")) {

            list = askForLeaveDao.findByReasonLike(info[1]);

        } else if (info[0].equals("status")) {

            list = askForLeaveDao.findByStatusLike(info[1]);

        } else if (info[0].equals("userName")) {

            list = askForLeaveDao.findByUserNameLike(info[1]);

        } else if (info[0].equals("nickName")) {

            list = askForLeaveDao.findByNickNameLike(info[1]);
        }


        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 查询所有学生信息
     */
    @Override
    public Result queryAllStudentInfo() {
        // TODO 查询所有学生信息
        List<Map<String, Object>> list = askForLeaveDao.queryAllStudentInfo();
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 查询所有学生请假信息
     */
    @Override
    public Result queryAllStudentAndAskForLeaveInfo() {
        // TODO 查询所有学生请假信息
        List<Map<String, Object>> list = askForLeaveDao.queryAllStudentAndAskForLeaveInfo();
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    @Override
    public Result selectAskForLeaveByUserId(Integer userId) {
        // TODO 根据学生ID查询学生请假信息
        List<Map<String, Object>> list = askForLeaveDao.selectAskForLeaveByUserId(userId);
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

}
